﻿using Common.Contract;
using Common.Contract.Customer.Identifier;
using Common.Contract.Customer.Record;
using Common.Contract.DataCenter.Record;
using Common.Contract.DataCenter.Request;
using Common.Service.Command;
using Common.Service.Queue;
using Common.Util;
using DataCenter.Model.Interface;
using DataCenter.Model.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataCenter.ServiceCommand
{
    public class UpdateProvinceServiceCommand : ProviderQueueCommand<IProviderFactory, ProvinceRecord, ResponseRecord>
    {
        public UpdateProvinceServiceCommand(IProviderFactory provider) : base(provider) { }
        protected override void Validate(ProvinceRecord request)
        {
            if (request == null) throw new ArgumentNullException("request");
        }

        protected override bool OnExecute(ResponseRecord response)
        {
            var province = Record;
            var obj = new Province
            {
                Id = new ProvinceIdentifier(province.Identifier).PersistableID,
                Name = province.ProvinceName,
                SoTransferFlag = BooleanString.TRUE,
                SoTransferDate = DateTime.Now,
                Lat = province.LAT,
                Lng = province.LNG,
                RegionId = province.RegionIdentifier,
            };
            var itemCheck = Providers.Province.RetrieveProvince(Record.Identifier);
            if (itemCheck == null)
            {
                var resultitem1 = Providers.Province.Add(obj);
            }
            else
            {
                var resultitem2 = Providers.Province.Update(obj);
            }
            return true;
        }
    }
}
